package game.helper;

import game.shared.Constants;

import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;

public class ShakeBackground {

//	Texture background;
	Texture textureMesh;
	Mesh squareMesh;
	int count = 0;
	float i = 0;
	float x = -Constants.SCREEN_WIDTH / 2;
	float y = -Constants.SCREEN_HEIGHT / 2;
	float sX = 480;
	float sY = 320;
	int nPhan=15;
	
	int nScaleMax = 8;
	int nScale = -nScaleMax;
	int p = 1;
	int kcSong = 3;
	Random r = new Random();
	Level level;
	
	public ShakeBackground(Level level){
		level = new Level();
		this.level = level;
		textureMesh = new Texture(Constants.BG_LEVEL_1);
//		textureMesh = background;
	    squareMesh = new Mesh(true, (nPhan+1)*2, (nPhan+1)*2,
	            new VertexAttribute(Usage.Position, 2, "a_position")
	            ,new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords")
	    );
	}
	
	public ShakeBackground(Level level, Texture textureMesh) {
		level = new Level();
		this.level = level;
		this.textureMesh = textureMesh;
	    squareMesh = new Mesh(true, (nPhan+1)*2, (nPhan+1)*2,
	            new VertexAttribute(Usage.Position, 2, "a_position")
	            ,new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords")
	    );
	}
	
	public void update(){
//		if (level.levelUp())
//			switch (Level.level) {
//			case 1:
//				textureMesh = new Texture(Constants.BG_LEVEL_1);
//				break;
//			case 2:
//				textureMesh = new Texture(Constants.BG_LEVEL_2);
//				break;
//			case 3:
//				textureMesh = new Texture(Constants.BG_LEVEL_3);
//				break;
//			case 4:
//				textureMesh = new Texture(Constants.BG_LEVEL_4);
//				break;
//	
//			default:
//				textureMesh = new Texture(Constants.BG_LEVEL_4);
//				break;
//		}
	}
	
	public void render(ShapeRenderer shapeRenderer){
		shapeRenderer.begin(ShapeType.Point);
		GLCommon gl = Gdx.gl;
	    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
//	    gl.glEnable(GL10.GL_DEPTH_TEST);
	    gl.glEnable(GL10.GL_TEXTURE_2D);
	    textureMesh.bind();
	    init();
	    squareMesh.render(GL10.GL_TRIANGLE_STRIP, 0, (nPhan+1)*2);
	    shapeRenderer.end();
	}
	
	private void init() {
		nScale += p;
		if((nScale >nScaleMax)||(nScale <-nScaleMax)){
			p*=-1;
		}
		float[]verticles = new float[(nPhan+1)*8];
		for(int i=0;i<nPhan+1;i++){
			int i2 = i*3;
			if(i>nPhan/3)
				i2 = nPhan;
			float scale = ((i%2-0.5f) * nScale)*(1-(float)i2/nPhan);
			verticles[i*8]=x+0+scale;	verticles[i*8+1]= y + (float)i/nPhan*sY; verticles[i*8+2]=0;	verticles[i*8+3]=(1-(float)(i)/nPhan)*320/512;
			verticles[i*8+4]=x+sX+scale;	verticles[i*8+5]= y + (float)i/nPhan*sY;	 verticles[i*8+6]=(float)480/512;	verticles[i*8+7]=(1-(float)i/nPhan)*320/512;
			kcSong--;
			if (kcSong < 0)
				kcSong = 0;
		}
		squareMesh.setVertices(verticles);
		short[]indices = new short[(nPhan+1)*2];
		for(short i=0;i<(nPhan + 1)*2; i++){
			indices[i] = i;
		}
	    squareMesh.setIndices(indices);
	}
}
